/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.opentoast.maven;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.eclipse.Messages;
import org.apache.maven.plugin.eclipse.writers.wtp.AbstractWtpResourceWriter;
import org.apache.maven.plugin.ide.IdeDependency;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;

/**
 * @author <a href="mailto:jiwhiz@gmail.com">Yuan Ji</a>
 *
 */
public class S2DMParSettingsWriter extends AbstractWtpResourceWriter
{
    /**
     * The .settings folder for config files.
     */
    private static final String DIR_WTP_SETTINGS = ".settings";

    /**
     * File name where s2dm par project configuration will be stored.
     */
    private static final String FILE_PAR_XML = "com.springsource.server.ide.runtime.core.par.xml";

    private static final String ELT_PAR = "com.springsource.server.ide.par:Par";
    
    private static final String ELT_BUNDLE = "bundle";
    
    private static final String ATTR_XMI_VERSION = "xmi:version";
    
    private static final String ATTR_XMLNS_XMI = "xmlns:xmi";
    
    private static final String ATTR_XMLNS_PAR = "xmlns:com.springsource.server.ide.par";
    
    private static final String ATTR_SYMBOLIC_NAME = "symbolicName";
    
    public void write() throws MojoExecutionException
    {
        // create a .settings directory (if not existing)
        File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
        settingsDir.mkdirs();

        Writer w;
        try
        {
            w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_PAR_XML ) ), "UTF-8" );
        }
        catch ( IOException ex )
        {
            throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
        }
        XMLWriter writer = new PrettyPrintXMLWriter( w );
        writeS2DMConfigForPar( writer );
        IOUtil.close( w );
    }

    /**
     * Writes out the facet info for a bundle project for S2DM.
     * 
     * @param writer
     */
    private void writeS2DMConfigForPar( XMLWriter writer)
    {
        writer.startElement( ELT_PAR );
        writer.addAttribute( ATTR_XMI_VERSION, "2.0" );
        writer.addAttribute( ATTR_XMLNS_XMI, "http://www.omg.org/XMI" );
        writer.addAttribute( ATTR_XMLNS_PAR, "http:///com/springsource/server/ide/par.ecore" );
        
        for ( int j = 0; j < config.getDepsOrdered().length; j++ )
        {
            IdeDependency dep = config.getDepsOrdered()[j];
            if ( dep.isReferencedProject() )
            {
                writer.startElement( ELT_BUNDLE );
                writer.addAttribute( ATTR_SYMBOLIC_NAME, dep.getEclipseProjectName() );
                writer.endElement();
            }
        }
        
        writer.endElement(); // faceted-project
    }
    
}
